#!/bin/sh -efu
#
# Check regeneration of whole repo index.
#
# Copyright (C) 2021 Gleb Fotengauer-Malinovskiy <glebfm@altlinux.org>
# All rights reserved.
#
# SPDX-License-Identifier: GPL-2.0-or-later

. "${srcdir=.}/init.sh"

[ -d "${ART_HEAVY_TESTDATA_DIR:-}" ] ||
	skip_ 'heavy test data is not found in $ART_HEAVY_TESTDATA_DIR'

NAME="$1"; shift

from_repo="$ART_HEAVY_TESTDATA_DIR"/"$NAME"/latest

[ -d "$from_repo" ]

repo_expected=$from_repo

skip_compare_release=yes
strip_size_md5=yes
basedir=base.bloat

for arch in x86_64 noarch; do
	copy_pkgs "$arch"

	comps="$(set +f && cd "$repo/$arch" && ls -d RPMS.?* | sed 's/^RPMS\.//')"
	[ -n "$comps" ]

	parse_release "$from_repo/$arch/$basedir"/release
	architectures="$_Architectures"
	codename="$_Codename"
	description="$_Description"
	label="$_Label"
	origin="$_Origin"
	suite="$_Suite"

	parse_release "$from_repo/$arch/$basedir"/release.classic
	archive="$_Archive"
	version="$_Version"

	$GENBASEDIR \
		--no-bz2 --no-xz \
		--bloat \
		--basedir "$basedir" \
		--architecture="$arch" \
		--architectures="$architectures" \
		--archive="$archive" \
		--codename="$codename" \
		--description="$description" \
		--label="$label" \
		--origin="$origin" \
		--suite="$suite" \
		--version="$version" \
		\
		--create \
		--architecture="$arch" \
		--topdir="$repo" \
		--flat --mapi \
		--no-oldhashfile \
		--no-scan \
		--changelog-since=2009-01-01 \
		$arch $comps

	check_base "$arch"
done

export XZ_OPT='--lzma2=nice=128,depth=80,lc=4'
basedir=base
for arch in x86_64 noarch; do
	comps="$(set +f && cd "$repo/$arch" && ls -d RPMS.?* | sed 's/^RPMS\.//')"
	[ -n "$comps" ]

	parse_release "$from_repo/$arch/$basedir"/release
	architectures="$_Architectures"
	codename="$_Codename"
	description="$_Description"
	label="$_Label"
	origin="$_Origin"
	suite="$_Suite"

	parse_release "$from_repo/$arch/$basedir"/release.classic
	archive="$_Archive"
	version="$_Version"

	$GENBASEDIR \
		--prev-base "$repo/$arch"/base.bloat \
		--useful-files "$from_repo"/useful-files \
		\
		--architecture="$arch" \
		--architectures="$architectures" \
		--archive="$archive" \
		--codename="$codename" \
		--description="$description" \
		--label="$label" \
		--origin="$origin" \
		--suite="$suite" \
		--version="$version" \
		\
		--create \
		--architecture="$arch" \
		--topdir="$repo" \
		--flat --mapi \
		--no-oldhashfile --bz2only \
		--no-scan \
		--changelog-since=2009-01-01 \
		--xz --no-bz2 \
		$arch $comps

	check_base "$arch"
done
